package com.baizhi.controller;

import com.baizhi.entity.Section;
import com.baizhi.service.SectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class SectionController {
    @Autowired
    private SectionService sectionService;
    @GetMapping("/sections")
    public Map showPageUsers(@RequestParam(defaultValue = "1")Integer page,
                             @RequestParam(defaultValue = "5")Integer limit,
                             @RequestParam(defaultValue = "")String search){
        List<Section> sections = sectionService.selectSection(page, limit, search);
        int totalRows = sectionService.totalRows(search);
        Map map = new HashMap();
        map.put("count",totalRows);
        map.put("data",sections);
        map.put("code",0);
        return map;
    }

    @DeleteMapping("/sections")
    public Map removeAnyPersons(@RequestBody List<Integer> ids) throws IOException {
        Map map = new HashMap();
        try{
            sectionService.deleteAnySection(ids);
            map.put("status", "success");
        }catch (Exception e){
            e.printStackTrace();
            map.put("status", "error");
        }
        return map;
    }
    //科室表编号
    @PostMapping("/sections")
    public Map addSection(@RequestBody Section section) throws IOException {
        System.out.println("section = " + section);
        Map map = new HashMap();

        try {
            sectionService.insertSection(section);
            map.put("status", "success");
        }catch (Exception e){
            e.printStackTrace();
            map.put("status", "error");
        }
        return map;

    }
    @DeleteMapping("/sections/{sectionId}")
    public Map removePersonsById(@PathVariable("sectionId") Integer sectionId) throws IOException {
        System.out.println("-----------------------------------");
        System.out.println("id = " + sectionId);
        Map map = new HashMap();
        try{
            sectionService.deleteById(sectionId);
            map.put("status", "success");
        }catch (Exception e){
            e.printStackTrace();
            map.put("status", "error");
        }
        return map;
    }

    @PutMapping("/sections/{id}")
    public Map updateSection(@PathVariable("id")Integer id,
                             @RequestBody Section s){
        Map map = new HashMap();
        try{
            sectionService.updateSection(s);
            map.put("status", "success");
        }catch (Exception e){
            e.printStackTrace();
            map.put("status", "error");
        }
        return map;
    }


}
